﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Guada
{
    public partial class FrmSolicitudHC : Form
    {
        public FrmSolicitudHC()
        {
            InitializeComponent();
        }

        private void FrmSolicitudHC_Load(object sender, EventArgs e)
        {
            Ventana(1);
        }

        private void BtnProcesar_Click(object sender, EventArgs e)
        {
            //DataTable Dt = (DataTable)dgHistorial.DataSource;
        }

        private void BtnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Ventana(int sform)
        {
            switch (sform)
            {
                case 1: //Seteo para Búsqueda
                    this.Size = new System.Drawing.Size(554, 143);
                    break;
                case 2: //Seteo para Mostrar Historial
                    this.Size = new System.Drawing.Size(554, 455);
                    this.GrpInfoHistorial.Size = new System.Drawing.Size(516, 270);
                    GrpInfoHistorial.Visible = true;
                    dgHistorial.Visible = true;
                    BtnCancelar.Visible = true;
                    //BtnProcesar.Visible = true;
                    break;
                case 3: //Seteo por Historial No Encontrado
                    this.Size = new System.Drawing.Size(554, 219);
                    this.GrpInfoHistorial.Size = new System.Drawing.Size(516, 77);
                    GrpInfoHistorial.Visible = true;
                    dgHistorial.Visible = false;
                    //lblInfo.Text = "Paciente sin Historial";
                    lblInfo.Visible = true;
                    break;
                default:
                    break;
            }
        }

        private void CargarDgHistorial(DataTable Dt)
        {
            try
            {
                dgHistorial.DataSource = Dt;
                DataGridViewColumn col;
                dgHistorial.AutoGenerateColumns = false;
                dgHistorial.Columns.Clear();

                //col = crearColumna()

                col = new DataGridViewTextBoxColumn();
                col.DataPropertyName = "IDPROFESIONAL";
                col.Name = "IDPROFESIONAL";
                col.Width = 100;
                col.HeaderText = "Id Profesional";
                col.ReadOnly = true;
                dgHistorial.Columns.Add(col);
                col.Visible = false;

                col = new DataGridViewTextBoxColumn();
                col.DataPropertyName = "PROFESIONAL";
                col.Name = "PROFESIONAL";
                col.Width = 150;
                col.HeaderText = "PROFESIONAL";
                col.ReadOnly = true;
                dgHistorial.Columns.Add(col);

                col = new DataGridViewTextBoxColumn();
                col.DataPropertyName = "NROMATRICULA";
                col.Name = "NROMATRICULA";
                col.Width = 80;
                col.HeaderText = "MATRICULA";
                col.ReadOnly = true;
                dgHistorial.Columns.Add(col);

                col = new DataGridViewTextBoxColumn();
                col.DataPropertyName = "ESPECIALIDAD";
                col.Name = "ESPECIALIDAD";
                col.Width = 120;
                col.HeaderText = "ESPECIALIDAD";
                col.ReadOnly = true;
                dgHistorial.Columns.Add(col);

                col = new DataGridViewTextBoxColumn();
                col.DataPropertyName = "DESCRIPCION";
                col.Name = "DESCRIPCION";
                col.Width = 250;
                col.HeaderText = "DIAGNOSTICO";
                col.ReadOnly = true;
                dgHistorial.Columns.Add(col);

                if (((DataTable)dgHistorial.DataSource).Rows.Count > 0)
                    dgHistorial.Rows[0].Selected = true;
            }
            catch { }
        }

        //private DataGridViewColumn crearColumna(String propertyName, String colName, )
        //{
        //    DataGridViewColumn col;
        //    col = new DataGridViewTextBoxColumn();
        //    col.DataPropertyName = "IDHISTORIACLINICA";
        //    col.Name = "IDHISTORIACLINICA";
        //    col.Width = 1;
        //    col.HeaderText = "Historia Clinica";
        //    col.ReadOnly = true;
        //    dgHistorial.Columns.Add(col);
        //    col.Visible = false;
        //    return col;
        //}

        private void button1_Click(object sender, EventArgs e)
        {
            FrmBuscarPaciente buscar = new FrmBuscarPaciente(true);
            buscar.ShowDialog();
            txtNumDoc.Text = buscar.GetDocumento();
            buscar.Close();
        }

        private void dgHistorial_CellContentDoubleClick_1(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string sParam = dgHistorial.Rows[dgHistorial.CurrentRow.Index].Cells[1].Value.ToString();
                FrmDetalleHC Detalle = new FrmDetalleHC(sParam);
                Detalle.ShowDialog();
            }
            catch { }
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            FacProFacu.HistoriaClinica Historial = new FacProFacu.HistoriaClinica();
            DataTable Dt = new DataTable();
            Dt = Historial.GetHistoriaSolicitada(txtNumDoc.Text).Tables["HCI"];
            if (Dt.Rows.Count > 0)
            {
                Ventana(2);
                CargarDgHistorial(Dt);
            }
            else
            {
                Ventana(3);
                MessageBox.Show("El paciente no tiene Historia Clinica", "INFORMACION", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNumDoc.Focus();
            }
        }
    }
}
